<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\App\BaseController;
use App\Models\User;
use App\Models\Code;
use App\Models\Shop;
use App\Models\ShopUserCenter;

class UserInfoController extends BaseController
{

    /**
     * 改变手机号
     *
     * @return json
     */
    public function updateTel(Request $request)
    {
        $tel      = $request->input('tel', '');
        $code     = $request->input('code', '');
        $user_id  = session('user_id');

        //手机验证处理
        if( empty( $tel ) ){

            return self::error('手机号码不可为空');

        }else{

            $user = User::where('user_mobile', $tel)->first();

            if($user) return self::error('手机已被绑定');

        }

        //验证码有效期验证
        if( !empty($code) ){

           $check_res = Code::check_code($tel, $code, 'bind');

           if(!$check_res) return self::error('验证码错误或过期');

        }else{

            return self::error('验证码不可为空');

        }

        $user = User::find($user_id);

        $user->user_name     = $tel;
        $user->user_mobile   = $tel;

        $user->save();

        return self::success('手机绑定成功');
    }

    /**
     * 修改个人资料
     *
     * @return json
     */
    public function update_user_info(Request $request)
    {
        $user_id  = session('user_id');

        $user = User::find($user_id);

        //设置昵称
        if($request->input('nickname')){

            $value = trim($request->input('nickname'));

            $user->user_nickname = $value;
        }

        //设置年龄
        if($request->input('age')){

            $value = trim($request->input('age'));

            $user->user_age = $value;
        }

        //设置性别
        if($request->input('sex')){

            $value = trim($request->input('sex'));
            
            $user->user_sex = $value;
        }

        //设置常驻地
        if($request->input('avatar')){

            $value = trim($request->input('avatar'));

            $user->user_avatar = $value;
        }

        $user->save();

        return self::success('设置成功');

    }

    /**
     * 个人中心
     *
     * @return json
     */
    public function personal_center(Request $request)
    {
        $user_id  = session('user_id');

        $info = User::find($user_id);

        $return = [];

        if($info){

            $return['id']            = $info->id;//用户id
            $return['user_mobile']   = $info->user_mobile;//手机号
            $return['nickname']      = $info->user_nickname;//昵称
            $return['sex']           = $info->user_sex; //性别
            $return['age']           = $info->user_age; //年龄
            $return['avatar']        = $info->user_avatar ? $info->user_avatar.'?x-oss-process=image/circle,r_200/format,png' : null;//头像
            $return['invite_code']   = $info->get_invite_code($info->id);//邀请码
            $return['into_queque']   = $info->into_queque;//0不可进入商铺队列，1可进入商铺队列
            $return['shop_id']       = $info->shop_id;//店铺id
            $return['is_real_name']  = $info->is_real_name($info->id);//0未实名认证 1实名认证
            $return['shop_status']   = Shop::where('id', $info->shop_id)->value('status') ? Shop::where('id', $info->shop_id)->value('status') : 2;//店铺状态1启用2禁用
            $return['rebate_points'] = $info->rebate_points;//贡献值
            $return['cover']         = "http://enclosure.oss-cn-hangzhou.aliyuncs.com/xinxiangmu/usercenter%281%29.png";//用户中心背景图

            $key = (string)'read_queque_'.$user_id;
            $return['is_first_queque'] = \Cache::get($key, 0);//获取用户是否阅读过协议


        }

        return self::json($return);
    }

    /**
     * 实名认证
     * 
     * @return json
     */
    public function realNameCheck(Request $request)
    {
        $user_id   = session('user_id');
        $real_name = trim($request->input('real_name',''));
        $id_card   = trim($request->input('id_card',''));

        if (empty($real_name) || empty($id_card)) {
            return self::error('必填字段不能为空');
        }

        $res = User::idcard_checksum18($id_card);

        if ($res == false) {
            return self::error('身份证号无效');
        }

        $info = User::find($user_id);

        //如果用户模型为空,说明有异常
        if (empty($info)) {

            return self::error('用户登录状态异常');
        }

        $info->real_name = $real_name;
        $info->id_card   = $id_card;
        $res = $info->save();

        if ($res == true) {
            return self::success('认证成功');
        }
        return self::error('认证失败');
    }

    /**
     * 用户绑定店铺
     *
     * @return json
     */
    public function bind_shop(Request $request)
    {
        $user_id = session('user_id');

        $shop_id = $request->input('shop_id', 0);

        $return = [];

        $user = User::find($user_id);

        if(!$user) return self::error('用户信息无效');
        
        if($user->shop_id > 0) return self::error('用户已绑定店铺');

        //用户绑定店铺
        $user->shop_id = $shop_id;
        $user->save();

        // 通过属性获取信息, 如果不存在初始化一个新的实例
        $shop_info = ShopUserCenter::firstOrNew(['shop_id'=>$shop_id, 'user_id'=>$user->id]);
        $shop_info->status  = 1;//启用
        $shop_info->save();
        //禁用其他店铺的资金中心
        ShopUserCenter::where('user_id', $user_id)->where('shop_id', '!=', $shop_id)->where('status', 1)->update(['status'=>2]);

        return self::success('绑定成功');
    }

    /**
     * 设置修改支付密码
     *
     * @param Request $request
     * @return void
     */
    public function editPayPwd( Request $request )
    {
        $user_id = session('user_id');
        $tel     = $request->input('tel');
        $code    = $request->input('code');
        $pwd     = $request->input('pwd');
        $pwd1    = $request->input('pwd1');

        if( !empty($code) ){

           $check_res = Code::check_code($tel, $code, 'update_pay');

           if(!$check_res) return self::error('验证码错误或过期');

        }else{

            return self::error('验证码不可为空');

        }
        
        if ( !$pwd ) {
            return self::error( '密码不能为空' );
        }

        if ( $pwd != $pwd1 ) {
            return self::error( '两次密码不一致' );
        }

        $info = User::find($user_id);

        if ( !$info ) {
            return self::error('用户不存在');
        }



        $info->pay_pwd = $info->create_pwd( $pwd );

        $info->save();

        return self::success('支付密码设置成功');

    }

    /**
     * 设置用户阅读过协议
     *
     * @param Request $request
     * @return json
     */
    public function set_read_queque(Request $request)
    {
        $user_id = session('user_id');

        $key = (string)'read_queque_'.$user_id;

        \Cache::forever($key, 1);

        return self::success('设置成功');

    }



}
